const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');

async function crawlData() {
    try {
        // 获取省份页面并提取省份信息
        const response = await axios.get('https://xl.16888.com/city.html');
        const $ = cheerio.load(response.data);
        const provinces = [];
        $('.mod-province a').each((index, element) => {
            const dataId = $(element).attr('data-id');
            const dataTitle = $(element).attr('data-title');
            if (dataId && dataTitle) {
                provinces.push({ id: dataId, name: dataTitle });
            }
        });

        // 存储最终数据的数组
        const allData = [];

        // 遍历省份，获取每个省份的城市信息
        for (const province of provinces) {
            try {
                const cityResponse = await axios.get(`https://xl.16888.com/city-2024-08-${province.id}-0-0-0-0-1.html`);
                const $cityPage = cheerio.load(cityResponse.data);
                const cities = [];
                $cityPage('.J_a_city a').each((index, cityElement) => {
                    const cityDataId = $(cityElement).attr('data-id');
                    const cityDataTitle = $(cityElement).attr('data-title');
                    if (cityDataId && cityDataTitle) {
                        cities.push({ id: cityDataId, name: cityDataTitle });
                    }
                });
                allData.push({ province: province.name, cities });
                console.log(`成功获取省份 ${province.name} 的城市信息，共 ${cities.length} 个城市。`);
            } catch (error) {
                console.error(`获取省份${province.name}的城市信息失败:`, error);
            }
        }

        // 将数据保存为 JSON 文件
        fs.writeFileSync('data.json', JSON.stringify(allData, null, 2));
        console.log('数据已保存到 data.json 文件中。');
    } catch (error) {
        console.error('Error:', error);
    }
}

crawlData();